package org.apache.lucene.ocean.database;

import java.io.Serializable;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.Field.TermVector;

/**
 * Analogous to the Lucene Field with the value being an object instead of a string.
 * If the field is UN_TOKENIZED then the internal string value is prepended with
 * the type given
 * 
 */
public class OceanField implements Serializable {
  public final String name;
  public final Object object;
  public Field.Store store;
  public Field.Index index;
  public Field.TermVector termVector;
  public Analyzer analyzer;
  
  public String toString() {
    // TODO: manually create tostring to decode the encoded untokenized values
    ToStringBuilder builder = new ToStringBuilder(this);
    builder.append("name", name);
    builder.append("object", object);
    builder.append("store", store);
    builder.append("index", index);
    builder.append("termVector", termVector);
    builder.append("analyzer", analyzer);
    return builder.toString();
  }

  public Fieldable getFieldable() {
    String str = null;
    if (index == Field.Index.UN_TOKENIZED) {
      str = OceanObject.toInternalString(name, object);
    } else {
      str = object.toString();
    }
    if (termVector == null) termVector = TermVector.NO;
    return new Field(name, str, store, index, termVector);
  }

  public OceanField(String name, Object object, Store store, Index index) {
    this(name, object, store, index, null, null);
  }
  
  public OceanField(String name, Object object, Store store, Index index, Analyzer analyzer) {
    this(name, object, store, index, null, analyzer);
  }
  
  public OceanField(String name, Object object, Store store, Index index, TermVector termVector, Analyzer analyzer) {
    this.name = name;
    this.object = object;
    this.store = store;
    this.index = index;
    this.termVector = termVector;
    this.analyzer = analyzer;
  }

  public OceanField(String name, long value, Store store, Index index) {
    this(name, value, store, index, null, null);
  }

  public OceanField(String name, double value, Store store, Index index) {
    this(name, value, store, index, null, null);
  }
}